package LOGIN;

import java.security.PublicKey;
import java.sql.SQLOutput;
import java.util.*;
import java.util.concurrent.BlockingDeque;

public class loginTest {

    public static void main(String[] args) {

        while (true) {
            System.out.println("欢迎来到wk学生管理系统");
            Scanner sc = new Scanner(System.in);
            System.out.println("请选择操作1登录 2注册 3忘记密码");

            String choose = sc.next();
            switch (choose) {
                case "1" -> login();
                case "2" -> Registration();
                case "3" -> ForgotPassword();
                default -> System.out.println("没有这个选项,请重新输入");

            }
        }
    }

    public static ArrayList<User> list = new ArrayList<>();

    public static void login() {
        Scanner sc = new Scanner(System.in);

        loop:
        while (true) {
            int count = 0;
            if (count < 3) {
                System.out.println("请输入用户名");

                String username = sc.next();
                System.out.println("请输入密码");
                String password = sc.next();
//            while (true) {
                System.out.println("请输入验证码");
                String flag = generateCaptcha();
                System.out.println(flag);
                String VerificationCode = sc.next();
                if (VerificationCode.equalsIgnoreCase(flag)) {
                    for (int i = 0; i < list.size(); i++) {
                        if (username.equals(list.get(i).getUsername())) {
                            if (password.equals(list.get(i).getPassword())) {
                                System.out.println("登录成功");
                                StudentSystem studentSystem = new StudentSystem();
                                studentSystem.maintest();

                            } else {
                                System.out.println("用户名或密码错误，请重新输入");
                                count = count + 1;
                                System.out.println("您还剩" + (3 - count) + "次机会");

                            }

                            break ;
                        } else {
                            System.out.println("用户名未注册，请先注册");
                            break loop;
                        }
                    }
                } else {
                    System.out.println("验证码错误，请重新输入");
                }
            } else {
                System.out.println("系统已经锁死，请联系管理员，联系电话:123456");
                break loop;
            }

        }
    }

    //System.out.println("注册");

    public static void Registration() {

        Scanner sc = new Scanner(System.in);
        User user = new User();

        while (true) {

            System.out.println("请输入用户名");

            String username = sc.next();
            if (username.length() >= 3 && username.length() <= 15) {

                boolean flag1 = charcontain(username);
                if (flag1) {
                    boolean flag = containsusername(list, username);
                    if (flag) {
                        System.out.println("用户名已存在,请重新输入");
                    } else {
                        user.setUsername(username);
                        break;
                    }
                } else {

                    System.out.println("用户名只能是字母加数字的组合，但不能是纯数字");
                }

            } else {
                System.out.println("用户名长度不符合，用户名长度必须在3~15位之间，请重新输入用户名");
            }
//            boolean flag = containsusername(list, username);
//            if (flag) {
//                System.out.println("用户名已存在,请重新输入");
//            } else {
//                user.setUsername(username);
//                break;
//            }
        }
        while (true) {
            System.out.println("请输入用户密码");
            String password = sc.next();
            user.setPassword(password);
            System.out.println("请再次输入用户密码");
            String password1 = sc.next();
            if (password.equals(password1)) {
                user.setPassword(password);
                break;
            } else {
                System.out.println("两次输入的密码不相同，请重新输入");
            }
        }
        while (true) {
            System.out.println("请输入身份证号码");
            String idNumber = sc.next();
            boolean ValidIDNumber = isValidIDNumber(idNumber);
            if (ValidIDNumber) {
                user.setIdNumber(idNumber);
                break;

            } else {
                System.out.println("输入了身份证无效，请重新输入");
            }
        }
        while (true) {
            System.out.println("请输入手机号");
            String phoneNumber = sc.next();
            boolean ValidPhoneNumber = isValidPhoneNumber(phoneNumber);
            if (ValidPhoneNumber) {
                user.setPhoneNumber(phoneNumber);
                break;
            } else {
                System.out.println(" 无效手机号，请重新输入有效的手机号");

            }
        }
        list.add(user);
    }
// System.out.println("忘记密码");
    public static void ForgotPassword() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
       looc: for (int i = 0; i < list.size(); i++) {
            if (username.equals(list.get(i).getUsername())) {
                System.out.println("请输入身份证号");
                String idNumber = sc.next();
                System.out.println("请输入手机号");
                String phoneNumber = sc.next();
                System.out.println("请输入密码");
                String password = sc.next();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    if (idNumber.equals(list.get(i1).getIdNumber()) && phoneNumber.equals(list.get(i1).getPhoneNumber())) {

                        list.get(i1).setPassword(password);
                        break;

                    }else {
                        System.out.println("账号信息不匹配，修改失败");
                        break looc;
                    }

                }
            }
            System.out.println("用户名未注册");

        }


    }

    public static boolean contains(ArrayList<User> list, String name) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUsername().equals(name)) {
                return true;
            }


        }
        return false;
    }

    public static int indexOf(ArrayList<User> list, String name) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUsername().equals(name)) {
                return i;
            }
        }
        return -1;
    }

    public static boolean containsusername(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            if (username.equals(list.get(i).getUsername())) {
                return true;

            }

        }
        return false;
    }

    public static boolean charcontain(String username) {
        boolean hasLetter = false;
        boolean hasDigit = false;

        for (char c : username.toCharArray()) {
            if (!Character.isLetterOrDigit(c)) {
                return false;
            }
            if (Character.isLetter(c)) {
                hasLetter = true;
            }
            if (Character.isDigit(c)) {
                hasDigit = true;
            }
        }

        // 检查用户名是否至少包含一个字母和一个数字
        return hasLetter && hasDigit;
    }

    /*
     * 验证身份证号码是否有效
     *  idNumber 待验证的身份证号码
     *  如果身份证号码有效返回true，否则返回false
     */
    public static boolean isValidIDNumber(String idNumber) {
        // 检查长度是否为18位
        if (idNumber.length() != 18) {
            return false;
        }

        // 检查不能以0开头
        char firstChar = idNumber.charAt(0);
        if (firstChar == '0') {
            return false;
        }

        // 检查前17位必须都是数字
        for (int i = 0; i < 17; i++) {
            char currentChar = idNumber.charAt(i);
            if (!Character.isDigit(currentChar)) {
                return false;
            }
        }

        // 检查最后一位可以是数字，也可以是大写X或小写x
        char lastChar = idNumber.charAt(17);
        if (!Character.isDigit(lastChar) && lastChar != ('X') && lastChar != ('x')) {
            return false;
        }

        // 如果以上所有条件都满足，则认为身份证号码有效
        return true;
    }

    //验证手机号
    public static boolean isValidPhoneNumber(String phoneNumber) {
        if (phoneNumber.length() != 11) {
            return false;
        }
        if (phoneNumber.charAt(0) == 0) {
            return false;
        }
        for (int i = 0; i < phoneNumber.length(); i++) {
            char currentChar = phoneNumber.charAt(i);
            if (!Character.isDigit(currentChar)) {
                return false;
            }

        }
        return true;

    }

    public static String generateCaptcha() {
        Random random = new Random();
        StringBuilder captcha = new StringBuilder();

        // 生成四位随机字母
        for (int i = 0; i < 4; i++) {
            char randomChar = getRandomLetter(random);
            captcha.append(randomChar);
        }

        // 生成一位随机数字
        char randomDigit = getRandomDigit(random);
        captcha.append(randomDigit);

        // 将生成的字符随机打乱顺序
        captcha = shuffleString(captcha.toString());

        return captcha.toString();
    }

    /*
     * 获取随机字母（大小写均可）
     *
     * @param random 随机数生成器
     * @return 随机字母
     */
    private static char getRandomLetter(Random random) {
        int letterType = random.nextInt(2); // 0 for uppercase, 1 for lowercase
        if (letterType == 0) {
            return (char) ('A' + random.nextInt(26)); // A-Z
        } else {
            return (char) ('a' + random.nextInt(26)); // a-z
        }
    }

    /**
     * 获取随机数字
     *
     * @param random 随机数生成器
     * @return 随机数字
     */
    private static char getRandomDigit(Random random) {
        return (char) ('0' + random.nextInt(10)); // 0-9
    }

    /**
     * 随机打乱字符串的顺序
     *
     * @param str 原始字符串
     * @return 打乱顺序后的字符串
     */
    private static StringBuilder shuffleString(String str) {
        List<Character> characters = new ArrayList<>();
        for (char c : str.toCharArray()) {
            characters.add(c);
        }

        Collections.shuffle(characters);

        StringBuilder shuffledString = new StringBuilder();
        for (char c : characters) {
            shuffledString.append(c);
        }

        return shuffledString;
    }
}
